package com.ycz.pattern.behaviour.observerpattern;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @ClassName Subject
 * @Author yanchengzhi
 * @date 2023.10.20 01:57
 */
public class Subject {

    private int state;
    private List<Observer> observers = new ArrayList<>();

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
        this.notifyAllObservers();
    }
    public void attach(Observer observer) {
        observers.add(observer);
    }

    public void notifyAllObservers() {
        for (Observer observer : observers) {
            observer.update();
        }
    }
}
